package com.huidao.servlet.shoppingcart;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huidao.entity.Goods;
import com.huidao.entity.Shoppingcart;
import com.huidao.entity.User;
import com.huidao.service.GoodsService;
import com.huidao.service.GoodsServiceImpl;
import com.huidao.service.ShoppingcartService;
import com.huidao.service.ShoppingcartServiceImpl;

/**
 * Servlet implementation class AddShoppingcartServlet
 */
public class AddShoppingcartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddShoppingcartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Shoppingcart shoppingcart=new Shoppingcart();
		Goods goods=new Goods();
		goods.setGoodsAccount(Long.parseLong(request.getParameter("goodsAccount")));
		User user=(User) request.getSession().getAttribute("user");
		shoppingcart.setUser(user);
		goods.setGoodsId(Long.parseLong(request.getParameter("id")));
		shoppingcart.setGoods(goods);
		shoppingcart.setGoodsNum(Long.parseLong(request.getParameter("num")));
		
		GoodsService goodsService=new GoodsServiceImpl();
		
		//Long goodsAccount=Long.parseLong(request.getParameter("goodsAccount"));
		//Long id=Long.parseLong(request.getParameter("id"));
		
		 ShoppingcartService s=new ShoppingcartServiceImpl();
		 String data=s.addShoppingcart(shoppingcart);
		    if(data.equals("")){
		    	//goodsService.updateGoodsAccount(goods);
		    	request.getRequestDispatcher("GetGoodsListServlet").forward(request, response);
		    }else{
		    	response.getWriter().print(data);
		    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
